﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace ChatTools
{
    public static class Tools
    {
        public static bool IsSocketConnected(Socket socket, int id)
        {
            bool flag = false;
            bool blocking = socket.Blocking;
            try
            {
                string str = string.Format("{0}|{1}|{2}|{3}", 0, id.ToString(), "", "");
                byte[] buffer = Encoding.Default.GetBytes(str);
                //socket.Blocking = false;
                socket.Send(buffer);
                flag = true;
            }
            catch (SocketException ex)
            {
                if (ex.NativeErrorCode.Equals(10035))
                    flag = true;
            }
            finally
            {
                socket.Blocking = blocking;
            }
            return flag;
        }



        public static bool IsSocketConnectedServer(Socket socket)
        {
            bool flag = false;
            bool blocking = socket.Blocking;
            try
            {
                byte[] buffer = new byte[0];
                socket.Blocking = false;
                socket.Send(buffer);
                flag = true;
            }
            catch (SocketException ex)
            {
                if (ex.NativeErrorCode.Equals(10035))
                    flag = true;
            }
            finally
            {
                socket.Blocking = blocking;
            }
            return flag;
        }
    }
}
